package org.soarsky.wealthchengdu.db;

import org.soarsky.wealthchengdu.entity.NewsContentData;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 数据库操作
 * 
 * @author huang
 * 
 */
public class DBtool {

	private DBOpenHelper dbOpenHelper = null;

	private volatile static DBtool dbTool;

	public DBtool(Context context) {
		dbOpenHelper = new DBOpenHelper(context);
	}

	public static DBtool getDBTool(Context context) {
		if (dbTool == null) {
			synchronized (DBtool.class) {
				if (dbTool == null) {
					dbTool = new DBtool(context);
				}
			}
		}
		return dbTool;
	}

	/**
	 * 查询所有 根据 state类型
	 * 
	 * 
	 * */
	public Cursor select() {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from DATA ", null);
		// db.close();
		return cursor;
	}

	/**
	 * 查询是否有数据 根据 id类型
	 * 
	 * 
	 * */
	public Cursor select(String id) {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select idto from DATA where idto = ?",
				new String[] { id });
		// db.close();
		return cursor;
	}

	/**
	 * @author huang
	 * @param id
	 *            插入数据
	 */
	public synchronized void insertData(NewsContentData newsContentData) {

		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

		try {
			db.execSQL(
					"insert into "
							+ "DATA"
							+ "(idto,title,titleEn,subTitle,subTitleEn,img,imgEn,source,sourceEn,content,contentEn,admireNum,remarkNum,shareNum,addTime,pvNum,collectNum) "
							+ "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
					new Object[] { newsContentData.getId(),
							newsContentData.getTitle(),
							newsContentData.getTitleEn(),
							newsContentData.getSubTitle(),
							newsContentData.getSubTitleEn(),
							newsContentData.getImg(),
							newsContentData.getImgEn(),
							newsContentData.getSource(),
							newsContentData.getSourceEn(),
							newsContentData.getContent(),
							newsContentData.getContentEn(),
							newsContentData.getAdmireNum(),
							newsContentData.getRemarkNum(),
							newsContentData.getShareNum(),
							newsContentData.getAddTime(),
							newsContentData.getPvNum(),
							newsContentData.getCollectNum() });
		} catch (Exception e) {
		} finally {
			db.close();
		}
	}

	/**
	 * 修改单独的某一列
	 * 
	 * 
	 * */
	public void UpdataDataID(String id) {
		// SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		// try {
		// db.execSQL(
		// "update "
		// + "DATA"
		// + " set  idto=? where idto = ?",
		// new Object[] { bean.getIntroducestring(),
		// bean.getIntroducetext(), bean.getPackagename() });
		// } finally {
		// db.close();
		// }
	}

	/**
	 * @author huang
	 * @param id
	 * 
	 *            删除DATA数据（对应用户id下的对应实体id数据）
	 * @return
	 */
	public synchronized long deleteData(String id) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		// Cursor cursor = null;
		long count = 0;
		try {
			count = db.delete("DATA", "idto = ? ", new String[] { id });
			return count;
		} finally {
			// if (cursor != null){
			// cursor.close();
			// }
			db.close();
		}
	}
}
